package org.jsping.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jsping.entity.Music;
import org.jsping.service.impl.CollectionServiceImpl;
import org.jsping.service.impl.MusicServiceImpl;

import com.alibaba.fastjson.JSON;

/**
 * 负责用户的收藏模块
 * 1.请求收藏集合   2.请求删除某个用户的收藏  3.请求添加用户收藏   
 */
@WebServlet("/Collection.html")
public class CollectionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	CollectionServiceImpl cImpl = new CollectionServiceImpl();
	MusicServiceImpl mImpl = new MusicServiceImpl();

	public CollectionServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			System.out.println("进来了Collection");
			String opr = request.getParameter("opr");//获取判断值
			String getUid = request.getParameter("uid");//uid
			PrintWriter pw = response.getWriter();
			
			if(opr.equals("list")) {
				//获取 uid 找collection的records
				String getRecords = cImpl.getRecord(Integer.valueOf(getUid));
				String[] getrec = getRecords.split(",");
				Music music = null;
				List<Music> mList = new ArrayList<Music>();
				//如果是空的
				if(getRecords.equals("")) {
					pw.print(0);
					pw.flush();
					return;
				}
				//添加
				System.out.println("uid:>>"+getUid+"getRecords:>>>"+getRecords);
				for (int i = 0; i < getrec.length; i++) {
					music = mImpl.getMusicInfo(Integer.valueOf(getrec[i]));
					mList.add(music);
				}
				//转换成JSON
				String jsonStr = JSON.toJSONString(mList);
				//输出
				System.out.println(jsonStr);
				pw.print(jsonStr);
				pw.flush();
			}else if(opr.equals("del")) {
				//删除，要删除的mid
				String delmid = request.getParameter("mid");
				Integer deluid = Integer.valueOf(request.getParameter("uid"));
				//获取 records列表
				System.out.println("delmid:>>>"+delmid);
				
				String[] records = cImpl.getRecord(deluid).split(",");
				List<String> list = new ArrayList<String>();
				String newrec = "";
				System.out.println("长度:>>>"+records.length);
		        
				for (int i = 0; i < records.length; i++) {
					//如果相等
					if(records[i].equals(delmid)) {
						//是否是最后一个下标
						if(i==(records.length-1)) {
							break;
						}
						i++;
					}
					
					System.out.println(records[i]);
					newrec+=records[i];
				}
				String[] newrecords = newrec.split("");
				for (int i = 0; i < newrecords.length; i++) {
					newrec +=newrecords[i]+",";
					System.out.println("newrecords>>>"+newrecords[i]);
				}
				
				
		        System.out.println("mid>>>"+delmid);
				System.out.println("新的newrec:>>>"+newrec);
				//更新数据
				/*update(deluid,newrec,request,response);*/
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		doGet(request, response);
	}

	//更新
	public void update(Integer uid,String array,HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			if(cImpl.updateRecord(uid, array) > 0) {
				response.sendRedirect("index.jsp");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			Integer userid = Integer.valueOf(request.getParameter("uid"));
			String musicid = request.getParameter("mid");
			boolean flag = true;
			String sb = cImpl.getRecord(userid);//获取records
			String[] array = sb.split(",");//字符串拆分成数组
			
			for(int i = 0;i<array.length;i++){
				System.out.println("循环中的array:"+array.length);
				//是否存在相同mid，不存在进行修改
				if(array[i].equals(musicid)) {
					System.out.println("相同");
					flag = false;
					break;
				}
				if(array[0].isEmpty()) {
					sb+=musicid;
				}else {
					sb+=","+musicid;
				}
			}
			System.out.println("array[0]:"+array[0]);
			
			/*//String类型*/
			if(flag){
				int jie =cImpl.updateRecord(userid, sb);
				if(jie > 0){
					response.sendRedirect("Test/tet.jsp");
				}else {
					System.out.println("不成功");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
